package com.javabasic2.Shootclient;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JPanel;

public class UnitImage {

	public enum Type {
		PLAYER, ENERMY, BACKGROUND , BOOM
	};
	
	private Image img;
	private BoomImage boom;
	
	private int sizeX, sizeY;
	private int x, y;
	private int velocity = 0;
	private int lifePoint = 1;
	public int getLifePoint() {
		return lifePoint;
	}
	public void setLifePoint(int lifePoint) {
		this.lifePoint = lifePoint;
	}

	private JPanel panel;
	private MainFrame frame;
	private boolean enermy;
	private boolean life = true;
	private boolean crash = false;

	private Type type;
	private Toolkit tk;

	private String name;

	public UnitImage() {
		life = false;
	}
	public UnitImage(String imagePath, MainFrame frame, boolean enermy, Type type) {
		this.type = type;
		this.frame = frame;
		this.panel = frame.getPanel();
		this.enermy = enermy;
		tk = panel.getToolkit();
		Image img = tk.createImage(imagePath);
		setSize(img.getWidth(null), img.getHeight(null));
		init(img);
	}

	public UnitImage(Image img, MainFrame frame, boolean enermy, Type type) {
		this.type = type;
		this.frame = frame;
		this.panel = frame.getPanel();
		this.enermy = enermy;
		tk = panel.getToolkit();
		init(img);
	}
	
	public void init(Image img) {
		
		this.img = img;
		boom = new BoomImage(frame.getBoomImage(),frame);
	}
	public void setName(String name) {
		this.name = name;
	}

	public int getSizeX() {
		return sizeX;
	}

	public int getSizeY() {
		return sizeY;
	}

	public void setSize(int sizeX, int sizeY) {
		this.sizeX = sizeX;
		this.sizeY = sizeY;
	}

	public int getX() {
		return sizeX;
	}

	public int getY() {
		return sizeY;
	}

	public void setPos(int x, int y) {
		this.x = x;// - img.getWidth(panel) / 2;
		this.y = y;// - img.getHeight(panel) / 2;
	}
	public Point getCenter() {
		return new Point(x,y);
	}



	public void draw(Graphics g) {

		if(isLife())
		switch (type) {
		case PLAYER:
			g.drawString(name + frame.getBulletSize(), x - name.length() * 3 - 4, y - sizeY);
			g.drawImage(img, x - sizeX, y - sizeY, panel);
			break;
		case ENERMY:
			g.drawString(String.format("%d", lifePoint), x, y - sizeY);
			g.drawImage(img, x - sizeX, y - sizeY, panel);
			break;
		case BOOM:
			if (boom.isLife())
				boom.draw(g);
			else
				life = false;
			break;
		default:

			break;
		}
	}

//	public void draw(Graphics g, Image img) {
//		g.drawImage(img, x, y, 10, 25, Color.WHITE, panel);
//	}

	public void setVelocity(int velocity) {
		this.velocity = velocity;
	}

	public boolean isLife() {
		if(lifePoint <= 0) {
			setVelocity(0);
			boom.setPos(x, y);
			boom.setSize(100, 100);
			type = Type.BOOM;
		}
		if(life)
			if (x < -100 || x > frame.getWidth() + 100 || y < -100
					|| y > frame.getHeight() + 100)
				life = false;
		return life;
	}

	public boolean isCrash(Point p2, int damage) {
		Point p1 = getCenter();
		crash = false;
		if(life && Type.PLAYER != type) {
			double tmpX = p1.getX() - p2.getX();
			double tmpY = p1.getY() - p2.getY();
			if (Math.sqrt((tmpX) * (tmpX) + (tmpY) * (tmpY)) < (sizeX+sizeY)/2) {
				crash = true;
				lifePoint -= damage;
			} else { 
				crash = false;
			}
		}
		
		return crash;
	}
}
